package highTech.Thread;

public class TraditionalThreadCommunication {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		final Bussiness bussiness=new Bussiness();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i=1;i<=2;i++){
					bussiness.sub(i);
				}
				
			}
		}).start();

		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i=1;i<=2;i++){
					bussiness.main(i);
				}
				
			}
		}).start();
	}

}

/**
 * Ҫ�õ���ͬ���(����ͬ����)�����ɸ�����Ӧ�ù���ͬһ�������ϣ����������������˸���ۺͳ���Ľ�׳��
 * @author Administrator
 *
 */
class Bussiness{
	private boolean bShouldSub=false;
	public synchronized void sub(int i){
		if(!bShouldSub){//����ʹ��while  ��Ӱ�ȫ����Ϊ�п��Գ��ּٻ��ѵ��������ʱ���ж�һ�¸�Ӱ�ȫ
			try {
				this.wait();//ͬ������������this   �����ϵ�synchronized����ǵ�ǰ����this  ����Ҫ��֤һ��
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
			
		for(int j=1;j<=1;j++){
			System.out.println("sub thread sequence of "+j+",loop of"+i);
		}
		
		bShouldSub=false;
		this.notify();
	}
	
	public synchronized void main(int i){
		while(bShouldSub){
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		for(int j=1;j<=1;j++){
			System.out.println("main thread sequence of "+j+",loop of"+i);
		}
		bShouldSub=true;
		this.notify();
	}
}
